package com.wanxi.servlet.contact;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.service.ContactService;
import com.wanxi.service.Impl.ContactServiceImpl;
import com.wanxi.util.Tool;
import com.wanxi.vo.ContactVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/contact/update")
public class UpdateContactServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        乱码处理、传输格式处理
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");
//        获取前端传来的值
        Integer id=Tool.strToInt(req.getParameter("id"));
        String userName=req.getParameter("userName");
        Integer enable= Tool.strToInt(req.getParameter("enable"));
        String email=req.getParameter("email");
        String phone=req.getParameter("phone");
        String content=req.getParameter("content");
        String remark=req.getParameter("remark");
//        调用service处理
        ContactVO contactVO = ContactVO.builder()
                .id(id)
                .userName(userName)
                .email(email)
                .phone(phone)
                .content(content)
                .enable(enable)
                .remark(remark)
                .build();
        ContactService contactService=new ContactServiceImpl();
        ResultDTO resultDTO=contactService.updateContact(contactVO);
//        返回值给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
